package com.itestmini.testplatformbackend.config.intercepors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author moso
 * @Date 2024/12/24 22:45
 * 拦截器
 */

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 设置response的字符编码为UTF-8
        response.setCharacterEncoding("UTF-8");
        // 设置contentType为text/html，并指定UTF-8编码
        response.setContentType("application/json;charset=UTF-8");
        log.info("===preHandle===");
        log.info("===request.getRequestURI===" + request.getRequestURI());
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        log.info("===postHandle===");
        log.info("===request.getRequestURI===" + request.getRequestURI());
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        log.info("===afterCompletion ===");
        log.info("===request.getRequestURI===" + request.getRequestURI());
    }
}
